package com.softtown.modules.message.utils;

import com.softtown.common.enums.*;
import com.softtown.common.exception.ErrorCode;
import com.softtown.common.exception.RenException;
import com.softtown.modules.message.enums.IsAgentApplStatusEnums;
import com.softtown.modules.message.service.SysEmailService;
import com.softtown.modules.message.service.SysSmsService;
import com.softtown.modules.security.user.SecurityUser;
import com.softtown.modules.sys.entity.SysMessageEntity;
import com.softtown.modules.sys.entity.SysMsgSendWayEntity;
import com.softtown.modules.sys.entity.SysUserEntity;
import com.softtown.modules.sys.service.SysMessageService;
import com.softtown.modules.sys.service.SysMsgSendWayService;
import com.softtown.modules.sys.service.SysParamsService;
import com.softtown.modules.sys.service.SysUserService;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.text.MessageFormat;
import java.util.Date;

/**
 * 发送消息异步
 *
 * @author Dingyayong
 * @version v1.0
 * @create 2019-12-18 14:28
 **/
@Component
@Slf4j
@Async
public class SendMessageUtil {

    @Resource
    private SysMsgSendWayService sysMsgSendWayService;

    @Resource
    private SysParamsService sysParamsService;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysEmailService sysEmailService;

    @Resource
    private SysSmsService sysSmsService;

    @Resource
    private SysMessageService sysMessageService;




    /**
     * @Author: Ding Ya Yong
     * @Description: 售后单
     * @Date: 2022/1/13 21:44
     * @Param: [compId, receiverId, senderId, docTypName, applName, applId]
     * @return: void
     **/
    @Transactional(rollbackFor = Exception.class)
    public void sendSaleAfterMessage(Long compId,Long receiverId,Long senderId,String customerName,String realName,Long id){
        Date nowDate = new Date();
        SysMsgSendWayEntity sysMsgSendWayEntity = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.
                STTATION_LETTER.value(), MsgTypeEnum.SALE_AFTER.value());
        Object[] contentArray = new Object[]{customerName,id, realName};
        String content = MessageFormat.format(sysMsgSendWayEntity.getMsgTemplateContent(), contentArray);
        // 发送消息
        SysMessageEntity sysMessageEntity = new SysMessageEntity();
        sysMessageEntity.setMsgSendWayType(MessageSendWayEnum.APP.value());
        sysMessageEntity.setMsgCompId(compId);
        sysMessageEntity.setType(MsgTypeEnum.SALE_AFTER.value());
        sysMessageEntity.setCreateDate(nowDate);
        sysMessageEntity.setCreator(SecurityUser.getUser().getId());
        sysMessageEntity.setReceiverId(receiverId);
        // 0是未读，1是已读
        sysMessageEntity.setIsRead(0);
        sysMessageEntity.setSendDate(nowDate);
        sysMessageEntity.setSenderId(senderId);
        sysMessageEntity.setContent(content);
        sysMessageEntity.setTitle("售后单");
        sysMessageEntity.setUpdateDate(nowDate);
        sysMessageEntity.setUpdater(SecurityUser.getUser().getId());
        sysMessageEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
        sysMessageService.insert(sysMessageEntity);
    }



    @Transactional(rollbackFor = Exception.class)
    public void sendReportMessage(Long compId,Long receiverId,Long senderId,String customerName,String realName,String date){
        Date nowDate = new Date();
        SysMsgSendWayEntity sysMsgSendWayEntity = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.
                STTATION_LETTER.value(), MsgTypeEnum.REPORT_RESOURCE.value());
        Object[] contentArray = new Object[]{realName,date, customerName};
        String content = MessageFormat.format(sysMsgSendWayEntity.getMsgTemplateContent(), contentArray);
        // 发送消息
        SysMessageEntity sysMessageEntity = new SysMessageEntity();
        sysMessageEntity.setMsgSendWayType(MessageSendWayEnum.APP.value());
        sysMessageEntity.setMsgCompId(compId);
        sysMessageEntity.setType(MsgTypeEnum.REPORT_RESOURCE.value());
        sysMessageEntity.setCreateDate(nowDate);
        sysMessageEntity.setCreator(SecurityUser.getUser().getId());
        sysMessageEntity.setReceiverId(receiverId);
        // 0是未读，1是已读
        sysMessageEntity.setIsRead(0);
        sysMessageEntity.setSendDate(nowDate);
        sysMessageEntity.setSenderId(senderId);
        sysMessageEntity.setContent(content);
        sysMessageEntity.setTitle("报备");
        sysMessageEntity.setUpdateDate(nowDate);
        sysMessageEntity.setUpdater(SecurityUser.getUser().getId());
        sysMessageEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
        sysMessageService.insert(sysMessageEntity);
    }



    @Transactional(rollbackFor = Exception.class)
    public void sendServiceMessage(Long compId,Long receiverId,Long senderId,String statusName,String realName,Long id){
        Date nowDate = new Date();
        SysMsgSendWayEntity sysMsgSendWayEntity = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.
                STTATION_LETTER.value(), MsgTypeEnum.REPORT_RESOURCE.value());
        Object[] contentArray = new Object[]{realName,id, statusName};
        String content = MessageFormat.format(sysMsgSendWayEntity.getMsgTemplateContent(), contentArray);
        // 发送消息
        SysMessageEntity sysMessageEntity = new SysMessageEntity();
        sysMessageEntity.setMsgSendWayType(MessageSendWayEnum.APP.value());
        sysMessageEntity.setMsgCompId(compId);
        sysMessageEntity.setType(MsgTypeEnum.REPORT_RESOURCE.value());
        sysMessageEntity.setCreateDate(nowDate);
        sysMessageEntity.setCreator(SecurityUser.getUser().getId());
        sysMessageEntity.setReceiverId(receiverId);
        // 0是未读，1是已读
        sysMessageEntity.setIsRead(0);
        sysMessageEntity.setSendDate(nowDate);
        sysMessageEntity.setSenderId(senderId);
        sysMessageEntity.setContent(content);
        sysMessageEntity.setTitle("维修服务单");
        sysMessageEntity.setUpdateDate(nowDate);
        sysMessageEntity.setUpdater(SecurityUser.getUser().getId());
        sysMessageEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
        sysMessageService.insert(sysMessageEntity);
    }




    @Transactional(rollbackFor = Exception.class)
    public void sendAgreeMessage(Long compId,Long receiverId,Long senderId,String docTypName,String applName,Long applId){
        Date nowDate = new Date();
        SysMsgSendWayEntity sysMsgSendWayEntity = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.
                STTATION_LETTER.value(), MsgTypeEnum.APPROVAL_PENDING_APPROVAL.value());
        Object[] titleArray = new Object[]{docTypName};
        String title = MessageFormat.format(sysMsgSendWayEntity.getMsgTemplateTitle(), titleArray);
        Object[] contentArray = new Object[]{applName,docTypName, applId};
        String content = MessageFormat.format(sysMsgSendWayEntity.getMsgTemplateContent(), contentArray);
        // 发送消息
        SysMessageEntity sysMessageEntity = new SysMessageEntity();
        sysMessageEntity.setMsgSendWayType(MessageSendWayEnum.APP.value());
        sysMessageEntity.setMsgCompId(compId);
        sysMessageEntity.setType(MsgTypeEnum.APPROVAL_PASSED.value());
        sysMessageEntity.setCreateDate(nowDate);
        sysMessageEntity.setCreator(SecurityUser.getUser().getId());
        sysMessageEntity.setReceiverId(receiverId);
        // 0是未读，1是已读
        sysMessageEntity.setIsRead(0);
        sysMessageEntity.setSendDate(nowDate);
        sysMessageEntity.setSenderId(senderId);
        sysMessageEntity.setContent(content);
        sysMessageEntity.setTitle(title);
        sysMessageEntity.setUpdateDate(nowDate);
        sysMessageEntity.setUpdater(SecurityUser.getUser().getId());
        sysMessageEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
        sysMessageService.insert(sysMessageEntity);
    }




    /**
    * 费用待审批消息
    * @author Dingyayong
    * @date 2019/12/18
    * @param applUserEmplName 申请人姓名
    * @param receiverId 收件人id
    * @param senderId 发送者Id
    * @param compId 公司id
    * @param applDeptName 申请部门名称
    * @param docTypName 单据类型
    * @param applDocNum 申请单号
    */
    @Transactional(rollbackFor = Exception.class)
    public void submitSendMessage (String applUserEmplName,Integer isAgentAppl,String isAgentApplName, Long receiverId, Long senderId, Long compId,
                                    String applDeptName, String docTypName, String applDocNum) {
        Date nowDate = new Date();
        // 获取消息发送的方并填入值
        String applName ;
        // TODO 多语言
        if(isAgentAppl.equals(IsAgentApplStatusEnums.APPL_OTHER.value())){
            applName = applUserEmplName.concat("(").concat(isAgentApplName).concat("代").concat(")");
        }else {
            applName = applUserEmplName;
        }
        SysMsgSendWayEntity sysMsgSendWayEntity = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.
                STTATION_LETTER.value(), MsgTypeEnum.APPROVAL_PENDING_APPROVAL.value());
        Object[] titleArray = new Object[]{docTypName};
        String title = MessageFormat.format(sysMsgSendWayEntity.getMsgTemplateTitle(), titleArray);
        Object[] contentArray = new Object[]{applDeptName,applName,docTypName, applDocNum};
        String content = MessageFormat.format(sysMsgSendWayEntity.getMsgTemplateContent(), contentArray);
        // 发送消息
        SysMessageEntity sysMessageEntity = new SysMessageEntity();
        sysMessageEntity.setMsgSendWayType(MessageSendWayEnum.APP.value());
        sysMessageEntity.setMsgCompId(compId);
        sysMessageEntity.setType(MsgTypeEnum.APPROVAL_PENDING_APPROVAL.value());
        sysMessageEntity.setCreateDate(nowDate);
        sysMessageEntity.setCreator(SecurityUser.getUser().getId());
        sysMessageEntity.setReceiverId(receiverId);
        // 0是未读，1是已读
        sysMessageEntity.setIsRead(0);
        sysMessageEntity.setSendDate(nowDate);
        sysMessageEntity.setSenderId(senderId);
        sysMessageEntity.setContent(content);
        sysMessageEntity.setTitle(title);
        sysMessageEntity.setUpdateDate(nowDate);
        sysMessageEntity.setUpdater(SecurityUser.getUser().getId());
        sysMessageEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
        sysMessageService.insert(sysMessageEntity);
        // 判断是否发送短信
        String messageParam = sysParamsService.getValue("SMS_SERVICE_IS_USABLE");
        // 获取收件人用户信息
        SysUserEntity sysUserEntity = sysUserService.getByUserId(receiverId);
        // 发送短信
        if (messageParam.equals(YesOrNoEnum.YES.value())) {
            if (sysUserEntity.getMobile() != null) {
                SysMsgSendWayEntity messageSendWay = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.
                        SMS.value(), MsgTypeEnum.APPROVAL_PENDING_APPROVAL.value());
                String messageContent = applDeptName + applName +
                        "|" + docTypName + "|" + applDocNum;
                sysSmsService.sendTrvl(sysUserEntity.getMobile(), messageSendWay.getMsgTemplateCode(), messageContent);
            }
        }
        // 判断是否发送email
        String emailParam = sysParamsService.getValue("MAIL_SERVICE_IS_USABLE");
        // 判断是否发送邮件
        if (emailParam.equals(YesOrNoEnum.YES.value())) {
            if (sysUserEntity.getEmail() != null) {
                try {
                    // 获取EMAIL消息发送方式
                    SysMsgSendWayEntity sysEmailSendWayEntity = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.MAIL.value(),
                            MsgTypeEnum.APPROVAL_PENDING_APPROVAL.value());
                    Object[] emailTitleArray = new Object[]{SystemNameEnum.SYS_NAME_CN.value(), docTypName};
                    String emailTitle = MessageFormat.format(sysEmailSendWayEntity.getMsgTemplateTitle(), emailTitleArray);
                    Object[] emailContentArray = new Object[]{applDeptName,applName,
                            docTypName, applDocNum, SystemNameEnum.SYS_NAME_CN.value(), new Date()};
                    String emailContent = MessageFormat.format(sysEmailSendWayEntity.getMsgTemplateContent(), emailContentArray);
                    sysEmailService.sendEmailByTrvl(sysUserEntity.getEmail(), emailTitle, emailContent);
                } catch (Exception e) {
                    throw new RenException(ErrorCode.EMAIL_FAIL_SEND);
                }
            }
        }
    }

    /**
     * 报销撤回消息
     * @author Dingyayong
     * @date 2019/12/18
     * @param receiverId 收件人id
     * @param compId 公司id
     * @param docTypName 单据类型
     * @param applDocNum 申请单号
     */
    @Transactional(rollbackFor = Exception.class)
    public void withdrawMessage (Long receiverId, Long compId, String docTypName, String applDocNum) {
        Date nowDate = new Date();
        // 获取发送消息方式
        SysMsgSendWayEntity sysMsgSendWayEntity = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.
                STTATION_LETTER.value(), MsgTypeEnum.APPROVAL_WITHDRAW_THE_APPLICATION.value());
        Object[] titleArray = new Object[]{docTypName};
        String title = MessageFormat.format(sysMsgSendWayEntity.getMsgTemplateTitle(), titleArray);
        Object[] contentArray = new Object[]{docTypName, applDocNum};
        String content = MessageFormat.format(sysMsgSendWayEntity.getMsgTemplateContent(), contentArray);

        // 判断是否发送短信
        String messageParam = sysParamsService.getValue("SMS_SERVICE_IS_USABLE");
        // 获取收件人用户信息
        SysUserEntity sysUserEntity = sysUserService.getByUserId(receiverId);
        // 发送短信
        if (messageParam.equals(YesOrNoEnum.YES.value())) {
            if (sysUserEntity.getMobile() != null) {
                SysMsgSendWayEntity messageSendWay = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.
                        SMS.value(), MsgTypeEnum.APPROVAL_WITHDRAW_THE_APPLICATION.value());
                String messageContent = docTypName + "|" + applDocNum;
                sysSmsService.sendTrvl(sysUserEntity.getMobile(), messageSendWay.getMsgTemplateCode(), messageContent);
            }
        }
        // 判断是否发送email
        String emailParam = sysParamsService.getValue("MAIL_SERVICE_IS_USABLE");
        // 判断是否发送邮件
        if (emailParam.equals(YesOrNoEnum.YES.value())) {
            if (sysUserEntity.getEmail() != null) {
                try {
                    // 获取EMAIL消息发送方式
                    SysMsgSendWayEntity sysEmailSendWayEntity = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.MAIL.value(),
                            MsgTypeEnum.APPROVAL_WITHDRAW_THE_APPLICATION.value());
                    Object[] emailTitleArray = new Object[]{SystemNameEnum.SYS_NAME_CN.value(), docTypName};
                    String emailTitle = MessageFormat.format(sysEmailSendWayEntity.getMsgTemplateTitle(), emailTitleArray);
                    Object[] emailContentArray = new Object[]{docTypName, applDocNum, SystemNameEnum.SYS_NAME_CN.value(), new Date()};
                    String emailContent = MessageFormat.format(sysEmailSendWayEntity.getMsgTemplateContent(), emailContentArray);
                    sysEmailService.sendEmailByTrvl(sysUserEntity.getEmail(), emailTitle, emailContent);
                } catch (Exception e) {
                    throw new RenException(ErrorCode.EMAIL_FAIL_SEND);
                }
            }
        }
        // 发送消息
        SysMessageEntity sysMessageEntity = new SysMessageEntity();
        sysMessageEntity.setMsgSendWayType(MessageSendWayEnum.APP.value());
        sysMessageEntity.setMsgCompId(compId);
        sysMessageEntity.setType(MsgTypeEnum.APPROVAL_WITHDRAW_THE_APPLICATION.value());
        sysMessageEntity.setCreateDate(nowDate);
        sysMessageEntity.setCreator(SecurityUser.getUser().getId());
        sysMessageEntity.setReceiverId(receiverId);
        // 0是未读，1是已读
        sysMessageEntity.setIsRead(0);
        sysMessageEntity.setSendDate(nowDate);
        sysMessageEntity.setSenderId(SecurityUser.getUser().getId());
        sysMessageEntity.setContent(content);
        sysMessageEntity.setTitle(title);
        sysMessageEntity.setUpdateDate(nowDate);
        sysMessageEntity.setUpdater(SecurityUser.getUser().getId());
        sysMessageEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
        sysMessageService.insert(sysMessageEntity);
    }

    /**
     * 最终审批同意
     * @author Dingyayong
     * @date 2019/12/18
     * @param receiverId 收件人id
     * @param senderId 发送者Id
     * @param compId 公司id
     * @param docTypName 单据类型
     * @param applDocNum 申请单号
     */
    @Transactional(rollbackFor = Exception.class)
    public void finallyAgreed (Long receiverId, Long senderId, Long compId, String docTypName, String applDocNum) {
        Date nowDate = new Date();
        // 获取消息发送方式
        SysMsgSendWayEntity sysMsgSendWayEntity = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.
                STTATION_LETTER.value(), MsgTypeEnum.APPROVAL_PASSED.value());
        Object[] titleArray = new Object[]{docTypName};
        String title = MessageFormat.format(sysMsgSendWayEntity.getMsgTemplateTitle(), titleArray);
        Object[] contentArray = new Object[]{docTypName, applDocNum};
        String content = MessageFormat.format(sysMsgSendWayEntity.getMsgTemplateContent(), contentArray);

        // 判断是否发送短信
        String messageParam = sysParamsService.getValue("SMS_SERVICE_IS_USABLE");
        // 获取收件人用户信息
        SysUserEntity sysUserEntity = sysUserService.getByUserId(receiverId);
        // 发送短信
        if (messageParam.equals(YesOrNoEnum.YES.value())) {
            if (sysUserEntity.getMobile() != null) {
                SysMsgSendWayEntity messageSendWay = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.
                        SMS.value(), MsgTypeEnum.APPROVAL_PASSED.value());
                String messageContent = docTypName + "|" + applDocNum;
                sysSmsService.sendTrvl(sysUserEntity.getMobile(), messageSendWay.getMsgTemplateCode(), messageContent);
            }
        }
        // 判断是否发送email
        String emailParam = sysParamsService.getValue("MAIL_SERVICE_IS_USABLE");
        // 判断是否发送邮件
        if (emailParam.equals(YesOrNoEnum.YES.value())) {
            if (sysUserEntity.getEmail() != null) {
                try {
                    // 获取EMAIL消息发送方式
                    SysMsgSendWayEntity sysEmailSendWayEntity = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.MAIL.value(),
                            MsgTypeEnum.APPROVAL_PASSED.value());
                    Object[] emailTitleArray = new Object[]{SystemNameEnum.SYS_NAME_CN.value(), docTypName};
                    String emailTitle = MessageFormat.format(sysEmailSendWayEntity.getMsgTemplateTitle(), emailTitleArray);
                    Object[] emailContentArray = new Object[]{docTypName, applDocNum, SystemNameEnum.SYS_NAME_CN.value(), new Date()};
                    String emailContent = MessageFormat.format(sysEmailSendWayEntity.getMsgTemplateContent(), emailContentArray);
                    sysEmailService.sendEmailByTrvl(sysUserEntity.getEmail(), emailTitle, emailContent);
                } catch (Exception e) {
                    throw new RenException(ErrorCode.EMAIL_FAIL_SEND);
                }
            }
        }

        // 发送消息
        SysMessageEntity sysMessageEntity = new SysMessageEntity();
        sysMessageEntity.setMsgSendWayType(MessageSendWayEnum.APP.value());
        sysMessageEntity.setMsgCompId(compId);
        sysMessageEntity.setType(MsgTypeEnum.APPROVAL_PASSED.value());
        sysMessageEntity.setCreateDate(nowDate);
        sysMessageEntity.setCreator(SecurityUser.getUser().getId());
        sysMessageEntity.setReceiverId(receiverId);
        // 0是未读，1是已读
        sysMessageEntity.setIsRead(0);
        sysMessageEntity.setSendDate(nowDate);
        sysMessageEntity.setSenderId(senderId);
        sysMessageEntity.setContent(content);
        sysMessageEntity.setTitle(title);
        sysMessageEntity.setUpdateDate(nowDate);
        sysMessageEntity.setUpdater(SecurityUser.getUser().getId());
        sysMessageEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
        sysMessageService.insert(sysMessageEntity);
    }

    /**
     * 拒绝审批
     * @author Dingyayong
     * @date 2019/12/18
     * @param receiverId 收件人id
     * @param senderId 发送者Id
     * @param compId 公司id
     * @param docTypName 单据类型
     * @param applDocNum 申请单号
     */
    @Transactional(rollbackFor = Exception.class)
    public void rejectMessage (Long receiverId, Long senderId, Long compId, String docTypName, String applDocNum) {
        Date nowDate = new Date();
        // 获取消息发送方式
        SysMsgSendWayEntity sysMsgSendWayEntity = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.
                STTATION_LETTER.value(), MsgTypeEnum.APPROVAL_APPLICATION_REJECTED.value());
        Object[] titleArray = new Object[]{docTypName};
        String title = MessageFormat.format(sysMsgSendWayEntity.getMsgTemplateTitle(), titleArray);
        Object[] contentArray = new Object[]{docTypName, applDocNum};
        String content = MessageFormat.format(sysMsgSendWayEntity.getMsgTemplateContent(), contentArray);

        // 判断是否发送短信
        String messageParam = sysParamsService.getValue("SMS_SERVICE_IS_USABLE");
        // 获取收件人用户信息
        SysUserEntity sysUserEntity = sysUserService.getByUserId(receiverId);
        // 发送短信
        if (messageParam.equals(YesOrNoEnum.YES.value())) {
            if (sysUserEntity.getMobile() != null) {
                SysMsgSendWayEntity messageSendWay = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.
                        SMS.value(), MsgTypeEnum.APPROVAL_APPLICATION_REJECTED.value());
                String messageContent = docTypName + "|" + applDocNum;
                sysSmsService.sendTrvl(sysUserEntity.getMobile(), messageSendWay.getMsgTemplateCode(), messageContent);
            }
        }
        // 判断是否发送email
        String emailParam = sysParamsService.getValue("MAIL_SERVICE_IS_USABLE");
        // 判断是否发送邮件
        if (emailParam.equals(YesOrNoEnum.YES.value())) {
            if (sysUserEntity.getEmail() != null) {
                try {
                    // 获取EMAIL消息发送方式
                    SysMsgSendWayEntity sysEmailSendWayEntity = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.MAIL.value(),
                            MsgTypeEnum.APPROVAL_APPLICATION_REJECTED.value());
                    Object[] emailTitleArray = new Object[]{SystemNameEnum.SYS_NAME_CN.value(), docTypName};
                    String emailTitle = MessageFormat.format(sysEmailSendWayEntity.getMsgTemplateTitle(), emailTitleArray);
                    Object[] emailContentArray = new Object[]{docTypName, applDocNum, SystemNameEnum.SYS_NAME_CN.value(), nowDate};
                    String emailContent = MessageFormat.format(sysEmailSendWayEntity.getMsgTemplateContent(), emailContentArray);
                    sysEmailService.sendEmailByTrvl(sysUserEntity.getEmail(), emailTitle, emailContent);
                } catch (Exception e) {
                    throw new RenException(ErrorCode.EMAIL_FAIL_SEND);
                }
            }
        }

        // 发送消息
        SysMessageEntity sysMessageEntity = new SysMessageEntity();
        sysMessageEntity.setMsgSendWayType(MessageSendWayEnum.APP.value());
        sysMessageEntity.setMsgCompId(compId);
        sysMessageEntity.setType(MsgTypeEnum.APPROVAL_APPLICATION_REJECTED.value());
        sysMessageEntity.setCreateDate(nowDate);
        sysMessageEntity.setCreator(SecurityUser.getUser().getId());
        sysMessageEntity.setReceiverId(receiverId);
        // 0是未读，1是已读
        sysMessageEntity.setIsRead(0);
        sysMessageEntity.setSendDate(nowDate);
        sysMessageEntity.setSenderId(senderId);
        sysMessageEntity.setContent(content);
        sysMessageEntity.setTitle(title);
        sysMessageEntity.setUpdateDate(nowDate);
        sysMessageEntity.setUpdater(SecurityUser.getUser().getId());
        sysMessageEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
        sysMessageService.insert(sysMessageEntity);
    }

    /**
     * 审批退回
     * @author Dingyayong
     * @date 2019/12/18
     * @param receiverId 收件人id
     * @param senderId 发送者Id
     * @param compId 公司id
     * @param docTypName 单据类型
     * @param applDocNum 申请单号
     */
    @Transactional(rollbackFor = Exception.class)
    public void approvalBackMessage (Long receiverId, Long senderId, Long compId, String docTypName, String applDocNum){
        Date nowDate = new Date();
        // 发送消息
        // 获取消息发送方式
        SysMsgSendWayEntity sysMsgSendWayEntity = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.
                STTATION_LETTER.value(), MsgTypeEnum.APPROVAL_APPLICATION_RETURNED.value());
        Object[] titleArray = new Object[]{docTypName};
        String title = MessageFormat.format(sysMsgSendWayEntity.getMsgTemplateTitle(), titleArray);
        Object[] contentArray = new Object[]{docTypName, applDocNum};
        String content = MessageFormat.format(sysMsgSendWayEntity.getMsgTemplateContent(), contentArray);

        // 判断是否发送短信
        String messageParam = sysParamsService.getValue("SMS_SERVICE_IS_USABLE");
        // 获取收件人用户信息
        SysUserEntity sysUserEntity = sysUserService.getByUserId(receiverId);
        // 发送短信
        if (messageParam.equals(YesOrNoEnum.YES.value())) {
            if (sysUserEntity.getMobile() != null) {
                SysMsgSendWayEntity messageSendWay = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.
                        SMS.value(), MsgTypeEnum.APPROVAL_APPLICATION_RETURNED.value());
                String messageContent = docTypName + "|" + applDocNum;
                sysSmsService.sendTrvl(sysUserEntity.getMobile(), messageSendWay.getMsgTemplateCode(), messageContent);
            }
        }
        // 判断是否发送email
        String emailParam = sysParamsService.getValue("MAIL_SERVICE_IS_USABLE");
        // 判断是否发送邮件
        if (emailParam.equals(YesOrNoEnum.YES.value())) {
            if (sysUserEntity.getEmail() != null) {
                try {
                    // 获取EMAIL消息发送方式
                    SysMsgSendWayEntity sysEmailSendWayEntity = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.MAIL.value(),
                            MsgTypeEnum.APPROVAL_APPLICATION_RETURNED.value());
                    Object[] emailTitleArray = new Object[]{SystemNameEnum.SYS_NAME_CN.value(), docTypName};
                    String emailTitle = MessageFormat.format(sysEmailSendWayEntity.getMsgTemplateTitle(), emailTitleArray);
                    Object[] emailContentArray = new Object[]{docTypName,
                            applDocNum, SystemNameEnum.SYS_NAME_CN.value(), new Date()};
                    String emailContent = MessageFormat.format(sysEmailSendWayEntity.getMsgTemplateContent(), emailContentArray);
                    sysEmailService.sendEmailByTrvl(sysUserEntity.getEmail(), emailTitle, emailContent);
                } catch (Exception e) {
                    throw new RenException(ErrorCode.EMAIL_FAIL_SEND);
                }
            }
        }

        SysMessageEntity sysMessageEntity = new SysMessageEntity();
        sysMessageEntity.setMsgSendWayType(MessageSendWayEnum.APP.value());
        sysMessageEntity.setMsgCompId(compId);
        sysMessageEntity.setType(MsgTypeEnum.APPROVAL_APPLICATION_RETURNED.value());
        sysMessageEntity.setCreateDate(nowDate);
        sysMessageEntity.setCreator(SecurityUser.getUser().getId());
        sysMessageEntity.setReceiverId(receiverId);
        // 0是未读，1是已读
        sysMessageEntity.setIsRead(0);
        sysMessageEntity.setSendDate(nowDate);
        sysMessageEntity.setSenderId(senderId);
        sysMessageEntity.setContent(content);
        sysMessageEntity.setTitle(title);
        sysMessageEntity.setUpdateDate(nowDate);
        sysMessageEntity.setUpdater(SecurityUser.getUser().getId());
        sysMessageEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
        sysMessageService.insert(sysMessageEntity);
    }



    /**
     * 审批同意后撤回消息
     * @author Dingyayong
     * @date 2019/12/18
     * @param applUserEmplName 申请人姓名
     * @param receiverId 收件人id
     * @param compId 公司id
     * @param applDeptName 申请部门名称
     * @param docTypName 单据类型
     * @param applDocNum 申请单号
     */
    @Transactional(rollbackFor = Exception.class)
    public void agreeReturnSendMessage (String applUserEmplName, Long receiverId,  Long compId,
                                   String applDeptName, String docTypName, String applDocNum) {
        Date nowDate = new Date();
        // 站内信
        SysMsgSendWayEntity sysMsgSendWayEntity = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.
                STTATION_LETTER.value(), MsgTypeEnum.APPROVE_AGREE_RETURN.value());
        Object[] titleArray = new Object[]{docTypName};
        String title = MessageFormat.format(sysMsgSendWayEntity.getMsgTemplateTitle(), titleArray);
        Object[] contentArray = new Object[]{applDeptName,applUserEmplName,docTypName, applDocNum};
        String content = MessageFormat.format(sysMsgSendWayEntity.getMsgTemplateContent(), contentArray);
        // 发送消息
        SysMessageEntity sysMessageEntity = new SysMessageEntity();
        sysMessageEntity.setMsgSendWayType(MessageSendWayEnum.APP.value());
        sysMessageEntity.setMsgCompId(compId);
        sysMessageEntity.setType(MsgTypeEnum.APPROVE_AGREE_RETURN.value());
        sysMessageEntity.setCreateDate(nowDate);
        sysMessageEntity.setCreator(SecurityUser.getUser().getId());
        sysMessageEntity.setReceiverId(receiverId);
        // 0是未读，1是已读
        sysMessageEntity.setIsRead(0);
        sysMessageEntity.setSendDate(nowDate);
        sysMessageEntity.setContent(content);
        sysMessageEntity.setTitle(title);
        sysMessageEntity.setUpdateDate(nowDate);
        sysMessageEntity.setUpdater(SecurityUser.getUser().getId());
        sysMessageEntity.setUpdateTimestamp(Long.toString(System.currentTimeMillis()));
        sysMessageService.insert(sysMessageEntity);
        // 判断是否发送短信
        String messageParam = sysParamsService.getValue("SMS_SERVICE_IS_USABLE");
        // 获取收件人用户信息
        SysUserEntity sysUserEntity = sysUserService.getByUserId(receiverId);
        // 发送短信
        if (messageParam.equals(YesOrNoEnum.YES.value())) {
            if (sysUserEntity.getMobile() != null) {
                SysMsgSendWayEntity messageSendWay = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.
                        SMS.value(), MsgTypeEnum.APPROVE_AGREE_RETURN.value());
                String messageContent = applDeptName + applUserEmplName +
                        "|" + docTypName + "|" + applDocNum;
                sysSmsService.sendTrvl(sysUserEntity.getMobile(), messageSendWay.getMsgTemplateCode(), messageContent);
            }
        }
        // 判断是否发送email
        String emailParam = sysParamsService.getValue("MAIL_SERVICE_IS_USABLE");
        // 判断是否发送邮件
        if (emailParam.equals(YesOrNoEnum.YES.value())) {
            if (sysUserEntity.getEmail() != null) {
                try {
                    // 获取EMAIL消息发送方式
                    SysMsgSendWayEntity sysEmailSendWayEntity = sysMsgSendWayService.selectBySendTypeAndType(MsgSendWayTypeEnum.MAIL.value(),
                            MsgTypeEnum.APPROVE_AGREE_RETURN.value());
                    Object[] emailTitleArray = new Object[]{SystemNameEnum.SYS_NAME_CN.value(), docTypName};
                    String emailTitle = MessageFormat.format(sysEmailSendWayEntity.getMsgTemplateTitle(), emailTitleArray);
                    Object[] emailContentArray = new Object[]{applDeptName,applUserEmplName,
                            docTypName, applDocNum, SystemNameEnum.SYS_NAME_CN.value(), new Date()};
                    String emailContent = MessageFormat.format(sysEmailSendWayEntity.getMsgTemplateContent(), emailContentArray);
                    sysEmailService.sendEmailByTrvl(sysUserEntity.getEmail(), emailTitle, emailContent);
                } catch (Exception e) {
                    throw new RenException(ErrorCode.EMAIL_FAIL_SEND);
                }
            }
        }
    }
}